<template>
  <div class="container">
    <el-dialog v-model="state.dialog.isShowDialog" :title="state.dialog.titleObj[state.dialog.type]"
      label-position="top" width="600px">
      <el-form ref="dialogFormRef" :model="state.ruleForm" :rules="state.ruleRules" label-width="auto">
        <el-form-item label="用户名:" prop="username">
          <el-input v-model="state.ruleForm.username" placeholder="请输入" clearable :disabled="isDisabledFn" />
        </el-form-item>
        <el-form-item label="邮箱:" prop="email">
          <el-input v-model="state.ruleForm.email" placeholder="请输入" clearable :disabled="isDisabledFn" />
        </el-form-item>
        <el-form-item label="密码:" prop="password">
          <el-input v-model="state.ruleForm.password" type="password" placeholder="请输入" clearable
            :disabled="isDisabledFn" />
        </el-form-item>
        <el-form-item label="手机号:" prop="phone">
          <el-input v-model="state.ruleForm.phone" placeholder="请输入" clearable :disabled="isDisabledFn" />
        </el-form-item>
      </el-form>
      <template v-if="!isDisabledFn" #footer>
        <span class="dialog-footer">
          <el-button @click="onCancel" size="default">取 消</el-button>
          <el-button type="primary" size="default" @click="onSubmit">保 存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="addMsg">
import { reactive, onMounted, computed } from 'vue';
import { usersUpdate } from '/@/api/users/index';
import { accountTypeArr, accountStatusArr } from '/@/commmon/variate';

const emit = defineEmits(['updateList']);
const state = reactive({
  accountTypeArr: accountTypeArr,
  accountStatusArr: accountStatusArr,
  ruleForm: {
    username: '',
    email: '',
    password: '',
    phone: '',
  },
  ruleRules: {},
  dialog: {
    isShowDialog: false,
    row: null,
    type: '',
    titleObj: {
      'view': '查看',
      'edit': '编辑',
      'add': '新增'
    },
    isDisabled: false
  },
  loading: false
})
const onSubmit = async () => {
  const params = {
    ...state.ruleForm
  }
  console.log(params, "params");

  if (state.dialog.type === 'add') {
    console.log("新增用户人员");
  } else {
    console.log("编辑成功");
    usersUpdate(params).then((res) => {
      if (res.code === 10000) {
         const updateData = {
          ...state.dialog.row,
          ...state.ruleForm,
        };
        emit('updateItem', updateData);
        onCancel()
        // emit('updateList')
      }
    })
  }
}
const getViewDetail = () => {
  const params = {
    id: state.dialog.row.id
  }

}
const openDialog = (type, row) => {
  const typeArr = ['edit', 'view']
  state.dialog.row = row
  state.dialog.type = type
  closeDialog()
  console.log(row, "row0009");

  if (typeArr.includes(type)) {
    getViewDetail()
  }
  setTimeout(() => {
    if (row) {
      state.ruleForm = {
        ...state.ruleForm,
        ...row
      }
    } else {
      state.ruleForm = {
        username: '',
        email: '',
        password: '',
        phone: '',
      }
    }
    state.dialog.isShowDialog = true;
  }, 200)
};
const closeDialog = () => {
  state.ruleForm = {
    username: '',
    email: '',
    password: '',
    phone: '',
  }
};
const onCancel = () => {
  closeDialog();
  state.dialog.isShowDialog = false;
};
onMounted(() => { })
const isDisabledFn = computed(() => {
  return state.dialog.type === 'view'
})
defineExpose({
  openDialog,
})
</script>
<style scoped lang="scss">
.dialog-footer {
  display: flex;
  align-items: center;
  justify-content: center;
}

:deep(.el-form-item__content) {
  display: block;
}
</style>
